import pygame
from pygame.locals import *
from plane import *
from bullet import *
import time


def start():
    # 1.创建一个窗口，用来显示内容
    screen = pygame.display.set_mode((480, 700), 0, 32)

    # 2.创建一个和窗口大小的图片，用来充当背景
    image_file_path = './feiji/background.png'
    background = pygame.image.load(image_file_path).convert()

    # 3. 创建一个飞机对象
    hero_plane = HeroPlane(screen, "hero")
    # hero_group = pygame.sprite.Group(hero_plane)

    # 4. 创建一个敌人飞机
    enemy_plane = EnemyPlane(screen, "enemy")
    # enemy_group = pygame.sprite.Group(enemy_plane)

    # 4.把背景图片放到窗口中显示
    while True:
        screen.blit(background, (0, 0))
        hero_plane.display()
        enemy_plane.display()
        enemy_plane.move()
        enemy_plane.launch_bullet()
        # pygame.sprite.groupcollide(hero_group, enemy_group, True, True)

        # 判断是否是点击了退出按钮
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("exit")
                exit()
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_RIGHT]:
            print('right')
            hero_plane.move_right()
        elif keys_pressed[pygame.K_UP]:
            print('up')
            hero_plane.move_up()
        elif keys_pressed[pygame.K_DOWN]:
            print('down')
            hero_plane.move_down()
        elif keys_pressed[pygame.K_LEFT]:
            print('left')
            hero_plane.move_left()
        elif keys_pressed[pygame.K_SPACE]:
            print('space')
            hero_plane.launch_bullet()

        # 通过延时的方式，来降低这个while循环的循环速度，从而降低了CPU占用率
        time.sleep(0.01)

        pygame.display.update()


if __name__ == '__main__':
    start()
